package com.sgai.zero_robot.pop;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sgai.zero_robot.R;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.core.callback.BatteryCallback;


public class SettingBatteryView extends FrameLayout {
    private final String TAG = "CameraView";

    private TextView tvVoltageContent, tvCurrentContent, tvBatteryContent,tvHealthyContent
            ,tvCellTemperatureContent,tvMOSTemperatureContent,tvBMSSystem1Content,
            tvBMSSystem2Content,tvCellVoltage1Content,tvCellVoltage2Content,tvCellVoltage3Content
            ,tvCellVoltage4Content,tvCountContent;

    public SettingBatteryView(@NonNull Context context) {
        this(context, null);
    }

    public SettingBatteryView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SettingBatteryView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = inflate(context, R.layout.setting_battery, this);
        tvVoltageContent = view.findViewById(R.id.tv_voltage_content);
        tvCurrentContent = view.findViewById(R.id.tv_current_content);
        tvBatteryContent = view.findViewById(R.id.tv_battery_content);
        tvHealthyContent = view.findViewById(R.id.tv_healthy_content);
        tvCellTemperatureContent = view.findViewById(R.id.tv_cell_temperature_content);
        tvMOSTemperatureContent = view.findViewById(R.id.tv_mos_temperature_content);
        tvBMSSystem1Content = view.findViewById(R.id.tv_bms_system1_content);
        tvBMSSystem2Content = view.findViewById(R.id.tv_bms_system2_content);
        tvCellVoltage1Content = view.findViewById(R.id.tv_cell_voltage1_content);
        tvCellVoltage2Content = view.findViewById(R.id.tv_cell_voltage2_content);
        tvCellVoltage3Content = view.findViewById(R.id.tv_cell_voltage3_content);
        tvCellVoltage4Content = view.findViewById(R.id.tv_cell_voltage4_content);
        tvCountContent = view.findViewById(R.id.tv_count_content);

    }
    private void setContent(TextView tv,String content){
        if (tv!=null&&!TextUtils.isEmpty(content)){
            tv.setText(content);
        }
    }
    private BatteryCallback batteryCallback = new BatteryCallback() {
        @Override
        public void onBattery(int battery) {
            tvBatteryContent.setText(String.valueOf(battery));
        }

        @Override
        public void onVoltage(String voltage) {
            setContent(tvVoltageContent,voltage);
        }
        @Override
        public void onCurrent(String current) {
            setContent(tvCurrentContent,current);
        }

        @Override
        public void onHealthy(String healthy) {
            setContent(tvHealthyContent,healthy);
        }

        @Override
        public void onCellTemperature(String temperature) {
            setContent(tvCellTemperatureContent,temperature);
        }

        @Override
        public void onMOSTemperature(String temperature) {
            setContent(tvMOSTemperatureContent,temperature);
        }

        @Override
        public void onBMSSystem1(String state) {
            setContent(tvBMSSystem1Content,state);
        }

        @Override
        public void onBMSSystem2(String state) {
            setContent(tvBMSSystem2Content,state);
        }

        @Override
        public void onCellVoltage1(String voltage) {
            setContent(tvCellVoltage1Content,voltage);
        }

        @Override
        public void onCellVoltage2(String voltage) {
            setContent(tvCellVoltage2Content,voltage);
        }

        @Override
        public void onCellVoltage3(String voltage) {
            setContent(tvCellVoltage3Content,voltage);
        }

        @Override
        public void onCellVoltage4(String voltage) {
            setContent(tvCellVoltage4Content,voltage);
        }

        @Override
        public void onCount(String count) {
            setContent(tvCountContent,count);
        }
    };

    private void setValue(EditText et, String value) {
        if (et != null && !TextUtils.isEmpty(value)) {
            et.setText(value);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow: ");
        tvBatteryContent.setText(String.valueOf(RobotManagerFactory.getInstance().battery));
        setContent(tvVoltageContent,RobotManagerFactory.getInstance().batteryVolt);
        setContent(tvCurrentContent,RobotManagerFactory.getInstance().batteryCur);
        setContent(tvHealthyContent,RobotManagerFactory.getInstance().batterySoh);
        setContent(tvCellTemperatureContent,RobotManagerFactory.getInstance().batteryCellTemp);
        setContent(tvMOSTemperatureContent,RobotManagerFactory.getInstance().batteryMOSTemp);
        setContent(tvBMSSystem1Content,RobotManagerFactory.getInstance().batteryState1);
        setContent(tvBMSSystem2Content,RobotManagerFactory.getInstance().batteryState2);
        setContent(tvCellVoltage1Content,RobotManagerFactory.getInstance().batteryVol1);
        setContent(tvCellVoltage2Content,RobotManagerFactory.getInstance().batteryVol2);
        setContent(tvCellVoltage3Content,RobotManagerFactory.getInstance().batteryVol3);
        setContent(tvCellVoltage4Content,RobotManagerFactory.getInstance().batteryVol4);
        setContent(tvCountContent,RobotManagerFactory.getInstance().batteryCount);
        RobotManagerFactory.getInstance().addBatteryCallback(batteryCallback);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow: ");
        RobotManagerFactory.getInstance().removeBatteryCallback(batteryCallback);
    }
}
